﻿using System;
using CoreGraphics;
using Foundation;
using ObjCRuntime;
using UIKit;

namespace Sino.PagingView.Touch.Demo
{
	public partial class ViewController : UIViewController
	{
		protected ViewController(IntPtr handle) : base(handle)
		{

		}

		public override void ViewDidLoad()
		{
			base.ViewDidLoad();

			NavigationController.NavigationBar.Translucent = false;

			var host = new Host();

			host.Frame = View.Frame;

			host.ContentSize = new CGSize(View.Bounds.Width * 7, View.Bounds.Height);

			host.AddSubview(CreateView(View.Bounds.Width * 0, UIColor.White));
			host.AddSubview(CreateView(View.Bounds.Width * 1, UIColor.GroupTableViewBackgroundColor));
			host.AddSubview(CreateView(View.Bounds.Width * 2, UIColor.Gray));
			host.AddSubview(CreateView(View.Bounds.Width * 3, UIColor.Green));
			host.AddSubview(CreateView(View.Bounds.Width * 4, UIColor.Orange));
			host.AddSubview(CreateView(View.Bounds.Width * 5, UIColor.Purple));
			host.AddSubview(CreateView(View.Bounds.Width * 6, UIColor.Brown));



			var header = new PagingHeader(new CGRect(44, 100, UIScreen.MainScreen.Bounds.Width - 60, 36), host);
			View.AddSubview(host);
			//NavigationItem.LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Add);
			//NavigationItem.HidesBackButton = true;
			NavigationItem.TitleView = header;

			NavigationController.NavigationBar.Layer.ShadowOffset = new CGSize(0, 0);
			NavigationController.NavigationBar.Layer.ShadowColor = UIColor.Black.CGColor;
			NavigationController.NavigationBar.Layer.ShadowRadius = 8;
			NavigationController.NavigationBar.Layer.ShadowOpacity = 0.28f;

			var btn = new UIButton(new CGRect(0, 0, 13, 21));


			var back = new UIImageView();
			back.Frame = new CGRect(0, 0, 13, 21);
			back.TintColor = PagingHeader.HighlightTitleColor;
			back.Image = UIImage.FromBundle(@"UINavigationBarBackIndicatorDefault").ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);

			back.AddGestureRecognizer(new UITapGestureRecognizer(g =>
			{
				//NavigationController.PopViewController(true);
				host.ContentSize = new CGSize(3 * host.Bounds.Width, 0);
				host.Title = new string[] { "信息", "车辆", "登记" };

			}));

			NavigationItem.LeftBarButtonItem = new UIBarButtonItem(back);
		}

		private UIView CreateView(nfloat offsetX, UIColor color)
		{
			return new UIView(new CGRect(offsetX, 0, View.Frame.Width, View.Frame.Height)) { BackgroundColor = color };
		}

		public class Host : UIScrollView, IPagingHost, IUIScrollViewDelegate
		{
			string[] title = new string[] { "信息", "车辆", "登记", "附件", "费用", "应收", "定位" };

			public string[] Title { get { return title; } set { title = value; TitleChanged?.Invoke(this, null); } }


			public event EventHandler<ScrollEventArgs> Scrolling;
			public event EventHandler<ScrollEventArgs> IndexChanged;
			public event EventHandler TitleChanged;

			public void ScrollTo(int index, bool animte)
			{
				SetContentOffset(new CGPoint(index * Bounds.Width, 0), animte);
				IndexChanged?.Invoke(this, new ScrollEventArgs() { Index = index });
			}

			public Host()
			{
				InitView();
			}

			public Host(CGRect frame)
				: base(frame)
			{
				InitView();
			}

			protected virtual void InitView()
			{
				Delegate = this;
				PagingEnabled = true;
				Bounces = false;
			}

			[Export("scrollViewDidScroll:")]
			public new void Scrolled(UIScrollView scrollView)
			{
				Scrolling?.Invoke(this, new ScrollEventArgs() { Total = this.ContentSize.Width, OffsetX = ContentOffset.X, BoundWidth = Bounds.Width });
			}

			[Export("scrollViewDidEndDecelerating:")]
			public void DecelerationEnded(UIScrollView scrollView)
			{
				IndexChanged?.Invoke(this, new ScrollEventArgs() { Index = (int)(ContentOffset.X / Bounds.Width) });
			}
		}
	}
}

